#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    extern char **environ;
    char* argv[] = { "ls", "-l", "--color", NULL };
    pid_t id = fork();
    if (id < 0) { // fork error
        perror("fork");
        return 1;
    }
    else if (id == 0) { // child process
        int count = 5;
        while (count) {
            printf("I am child process.....:PID:%d, PPID:%d, count:%d\n", getpid(), getppid(),count);
            sleep(1);
            count--;
        }
        execve("/usr/bin/ls", argv, environ);
    }
    else { // father process
        while (1) {
            int status = 0;
            pid_t ret = waitpid(id, &status, WNOHANG);
            if (ret > 0) {
                printf("wait child process success\n");
                printf("child process exit code:%d\n", WEXITSTATUS(status));
                break;
            }
            else if (ret == 0) {
                printf("father process do his things.....\n");
                sleep(1);
            }
            else {
                printf("waitpid error...\n");
                break;
            }
        }
    }
    return 0;
}